package com.caigou.modules.gallery.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.openBrowser
import com.caigou.modules.gallery.adpter.GalleryPageAdapter
import com.caigou.modules.gallery.databinding.ActivityGalleryBinding
import com.caigou.modules.gallery.ui.fragment.CollectFragment
import com.caigou.modules.gallery.ui.fragment.IndexFragment
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/31
 */
@Route(path = RouterPath.Gallery.PAGE_GALLERY)
@AndroidEntryPoint
class GalleryActivity : BaseActivity() {

    private lateinit var binding: ActivityGalleryBinding
    var type = 1

    private val itemNames = mutableListOf(
        "图库索引",
        "我的收藏"
    )

    private val fragments = mutableListOf<Fragment>(
        IndexFragment(),
        CollectFragment()
    )

    private val pagerAdapter: GalleryPageAdapter by lazy {
        GalleryPageAdapter(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityGalleryBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.galleryLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)

        pagerAdapter.setList(fragments)
        binding.galleryViewPager2.adapter = pagerAdapter
        binding.galleryViewPager2.isUserInputEnabled = false
        TabLayoutMediator(binding.galleryTabLayout, binding.galleryViewPager2, true, true) { tab, position ->
            tab.text = itemNames[position]
        }.attach()
        binding.galleryViewPager2.offscreenPageLimit = itemNames.size

        binding.galleryRgTypeSelector.setOnCheckedChangeListener { group, _ ->
            type = when (group.checkedRadioButtonId) {
                binding.galleryBtnHk.id -> 1
                binding.galleryBtnMacao.id -> 2
                else -> 1
            }
            (fragments[0] as IndexFragment).fetchGalleryList()
            (fragments[1] as CollectFragment).fetchCollectList()
        }

        binding.galleryTvBack.setOnClickListener {
            finish()
        }

        binding.galleryTvOther.setOnClickListener {
            openBrowser("https://qntk9.com")
        }

    }
}